package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.AddCourseTeacherDto;
import com.xuecheng.content.model.dto.EditCourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Autowired
    private CourseBaseMapper courseBaseMapper;

    /**
     * 查询教师信息
     * @param courseId
     * @return
     */
    @Override
    public List<CourseTeacher> selectTeacherList(Long courseId) {
        // 根据课程id查询对应课程的教师
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(
                new LambdaQueryWrapper<CourseTeacher>()
                        .eq(CourseTeacher::getCourseId, courseId)
        );
        return courseTeachers;
    }

    /**
     * 添加教师信息
     * @param addCourseTeacherDto
     * @param companyId
     * @return
     */
    @Override
    public CourseTeacher insertTeacherInfo(AddCourseTeacherDto addCourseTeacherDto, Long companyId) {

        // 将Dto转为po
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(addCourseTeacherDto, courseTeacher);

        // 效验是否本机构课程
        checkCompanyId(companyId, courseTeacher.getCourseId(), "只允许向机构自己的课程中添加教师信息");

        // 添加创建时间
        courseTeacher.setCreateDate(LocalDateTime.now());

        // 写入数据库
        int insert = courseTeacherMapper.insert(courseTeacher);
        if(insert <= 0) {
            XueChengPlusException.cast("添加教师信息失败");
        }
        return getCourseTeacherInfo(courseTeacher.getId());
    }

    /**
     * 修改教师信息
     * @param editCourseTeacherDto
     * @param companyId
     * @return
     */
    @Override
    public CourseTeacher updateTeacherInfo(EditCourseTeacherDto editCourseTeacherDto, Long companyId) {

        // 获取教师id
        Long id = editCourseTeacherDto.getId();
        // 查询教师信息
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(id);
        if(courseTeacher == null) {
            XueChengPlusException.cast("教师不存在");
        }

        // 效验是否本机构课程
        checkCompanyId(companyId, courseTeacher.getCourseId(), "只允许向机构自己的课程中修改教师信息");

        // 封装数据
        BeanUtils.copyProperties(editCourseTeacherDto, courseTeacher);

        // 更新数据库
        int i = courseTeacherMapper.updateById(courseTeacher);
        if(i <= 0) {
            XueChengPlusException.cast("修改课程教师信息失败");
        }

        // 查看课程教师信息
        return getCourseTeacherInfo(courseTeacher.getId());
    }

    /**
     * 删除教师信息
     * @param courseId
     * @param courseTeacherId
     * @param companyId
     */
    @Override
    public void deleteTeacherInfo(Long courseId, Long courseTeacherId, Long companyId) {
        // 效验是否本机构课程
        checkCompanyId(companyId, courseId, "只允许向机构自己的课程中删除教师信息");
        // 删除课程教师信息
        courseTeacherMapper.deleteById(courseTeacherId);
    }

    /**
     * 根据id批量删除
     * @param teacherIds
     */
    @Override
    public void deleteBatchIds(List<Long> teacherIds) {
        courseTeacherMapper.deleteBatchIds(teacherIds);
    }

    private void checkCompanyId(Long companyId, Long courseId, String message) {
        // 查询课程所属机构
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        // 数据合法性效验
        // 根据具体的业务逻辑去效验
        // 只允许向机构自己的课程中修改老师信息
        if(!companyId.equals(courseBase.getCompanyId())) {
            XueChengPlusException.cast(message);
        }
    }

    private CourseTeacher getCourseTeacherInfo(Long courseTeacherId) {
        // 根据id查询课程教师信息
        return courseTeacherMapper.selectById(courseTeacherId);
    }
}
